package com.qf.osmarea.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qf.common.baidu.info.CheckResult;
import com.qf.common.baidu.utils.BaiduTextCheckUtils;
import com.qf.common.core.entity.area.Area;
import com.qf.common.core.util.UserUtils;
import com.qf.osmarea.mapper.AreaMapper;
import com.qf.osmarea.param.DeleteParam;
import com.qf.osmarea.service.AreaService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Qu Xiaotong
 * @data 2025/6/11
 * @desc
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Resource
    private AreaMapper areaMapper;

    @Override
    public List<Area> getArea() {

        List<Area> areas = areaMapper.selectList(Wrappers.lambdaQuery(Area.class)
                .eq(Area::getUserId, UserUtils.getUid()));

        return areas;
    }

    @Override
    public void addArea(Area area) {

        area.setUserId(UserUtils.getUid());

        CheckResult checkResult = BaiduTextCheckUtils.textCheck(area.getName() + area.getAddress());
        if (checkResult.getConclusionType()!=1){
            throw new RuntimeException("内容违规");
        }


        areaMapper.insert(area);

    }

    @Override
    public void deleteArea(DeleteParam deleteParam) {
        int delete = areaMapper.delete(Wrappers.lambdaQuery(Area.class)
                .eq(Area::getAreaId, deleteParam.getAreaId())
                .eq(Area::getUserId, UserUtils.getUid()));
        System.out.println("delete:" + delete);

    }

    @Override
    public void updateArea(Area area) {

        if (area.getIsDefault() == 1){
            areaMapper.update(Wrappers.lambdaUpdate(Area.class)
                    .set(Area::getIsDefault, 0)
                    .eq(Area::getUserId, UserUtils.getUid()).eq(Area::getIsDefault, 1));
        }

        areaMapper.update(area, Wrappers.lambdaUpdate(Area.class)
                .eq(Area::getUserId, UserUtils.getUid())
                .eq(Area::getAreaId, area.getAreaId()));


    }
}
